package so.ifl.manager.staff;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.StringHelper;
import so.ifl.commons.tools.except.SessionTimeOutException;
import so.ifl.servcie.TrainerCarService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.CarUseRecordVo;
import so.ifl.vo.FuelBillsVo;
import so.ifl.vo.InterimFeeVo;
import so.ifl.vo.RepairFeeVo;
import so.ifl.vo.TrainerCarReportVo;
import so.ifl.vo.TrainerCarVo;

/**
 *create by aaron.lu
 *create at 2012-3-20 上午12:19:39
 *<mailto: atyuan.lu@gmail.com>
 **/
@Component("trainerCarInfoManager")
public class TrainerCarInfoManager {
	
	@Autowired
	private TrainerCarService trainerCarService;
	
	Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 统计报表
	 * create by aaron.lu
	 * create at 2013-4-12 上午11:42:48
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void sumTrainCarFee(Page<TrainerCarReportVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		
		String exam_car_number_ = (String)params.get("exam_car_number_");
		if (null == exam_car_number_) {
			params.put("exam_car_number_", "");
		}
		String office_code_ = (String)params.get("office_code_");
		if (null == office_code_) {
			params.put("office_code_", "");
		}
		Date begin_time = (Date)params.get("begin_time");
		if (null == begin_time) {
			params.put("begin_time", new Date());
		}
		Date end_time = (Date)params.get("end_time");
		if (null == end_time) {
			params.put("end_time", new Date());
		}
		
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			params.put("company_id", accountVo.getCompanyId());
			params.put("offset_", offset);
			params.put("limit_", limmit);
			
			List<TrainerCarReportVo> list = trainerCarService.sumTrainCarFee(params);
			int total = 1;
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("登录超时。。");
			
			page.setEntities(new ArrayList<TrainerCarReportVo>());
			page.setEntityCount(0);
		}
		
	}
	
	/**
	 * 查询教练车费用报表
	 * create by aaron.lu
	 * create at 2013-3-13 下午11:46:22
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void listTrainerCar(Page<TrainerCarReportVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		
		String exam_car_number_ = (String)params.get("exam_car_number_");
		if (null == exam_car_number_) {
			params.put("exam_car_number_", "");
		}
		String office_code_ = (String)params.get("office_code_");
		if (null == office_code_) {
			params.put("office_code_", "");
		}
		Date begin_time = (Date)params.get("begin_time");
		if (null == begin_time) {
			params.put("begin_time", new Date());
		}
		Date end_time = (Date)params.get("end_time");
		if (null == end_time) {
			params.put("end_time", new Date());
		}
		
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			params.put("company_id", accountVo.getCompanyId());
			params.put("offset_", offset);
			params.put("limit_", limmit);
			
			List<TrainerCarReportVo> list = trainerCarService.listTrainerCar(params);
			int total = trainerCarService.countTrainerCarWithParam(params);
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("登录超时。。");
			
			page.setEntities(new ArrayList<TrainerCarReportVo>());
			page.setEntityCount(0);
		}
		
	}
	
	
	/**
	 * 查询教练车信息
	 * create by aaron.lu
	 * create at 2012-3-20 下午3:55:12
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getTrainerCarInfo(Page<TrainerCarVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}

		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			params.put("company_id", accountVo.getCompanyId());
			
			List<TrainerCarVo> list = trainerCarService.queryTrainerCarWithParamAndPagination(params, limmit, offset);
			int countsql = trainerCarService.countTrainerCarWithParam(params);
			page.setEntities(list);
			page.setEntityCount(countsql);
		}else{
			logger_.error("登录超时。。");
			
			page.setEntities(new ArrayList<TrainerCarVo>());
			page.setEntityCount(0);
		}
	}
	
	/**
	 * 保存教练车
	 * create by aaron.lu
	 * create at 2012-3-21 下午10:29:46
	 * <mailto: atyuan.lu@gmail.com>
	 * @param trainerCars
	 *
	 */
	@DataResolver
	public void trainerCarHandler(Collection<TrainerCarVo> trainerCars){
		for (TrainerCarVo trainerCarVo : trainerCars) {
			EntityState entityState = EntityUtils.getState(trainerCarVo);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				insertTrainerCar(trainerCarVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				updateTrainerCar(trainerCarVo);
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				disableTrainerCar(trainerCarVo);
			}
		}
	}
	
	/**
	 * 查询使用记录
	 * create by aaron.lu
	 * create at 2012-3-22 下午5:39:37
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getCarUseRecord(Page<CarUseRecordVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}

		String trainer_car_id_ = (String)params.get("trainer_car_id_");
		if (StringHelper.isNotEmpty(trainer_car_id_)) {
			List<CarUseRecordVo> carUseRecordVos = trainerCarService.queryUseRecord(params, offset, limmit);
			int total = trainerCarService.countUseRecord(params);
			page.setEntities(carUseRecordVos);
			page.setEntityCount(total);			
		}else{
			page.setEntities(new ArrayList<CarUseRecordVo>());
			page.setEntityCount(0);
		}
		
		
	}
	
	/**
	 * 使用记录控制器
	 * create by aaron.lu
	 * create at 2012-3-22 下午5:48:40
	 * <mailto: atyuan.lu@gmail.com>
	 * @param carUseRecordVos
	 *
	 */
	@DataResolver
	public void carUseRecordHandler(Collection<CarUseRecordVo> carUseRecordVos){
		for (CarUseRecordVo carUseRecordVo : carUseRecordVos) {
			EntityState entityState = EntityUtils.getState(carUseRecordVo);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
				if (null != accountVo && null !=  accountVo.getAccoutId()) {
					carUseRecordVo.setCreateUser(accountVo.getAccountLoginName());
					carUseRecordVo.setCreateTime(new Date());
					carUseRecordVo.setLastModifyUser(accountVo.getAccountLoginName());
					carUseRecordVo.setLastModifyTime(new Date());
					carUseRecordVo.setCarUseUnit(carUseRecordVo.getCarUseType());//类型与使用单位一样
				}else{
					throw new SessionTimeOutException("登录超时。。");
				}
				trainerCarService.insertUseRecord(carUseRecordVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				//DO NOTHING
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				trainerCarService.deleteUseRecord(carUseRecordVo.getCarUseRecordId());
			}
		}
	}
	
	/**
	 * 油款信息提供方法
	 * create by aaron.lu
	 * create at 2013-3-11 下午8:58:22
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getFuelBills(Page<FuelBillsVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		
		Integer trainer_car_id_ = (Integer)params.get("trainer_car_id_");
		
		if (null != trainer_car_id_) {
			
			List<FuelBillsVo> list = trainerCarService.queryFuelBills(params, limmit, offset);
			int total = trainerCarService.countFuelBills(params);
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("无教练车ID");
			
			page.setEntities(new ArrayList<FuelBillsVo>());
			page.setEntityCount(0);
		}
	}
	
	/**
	 * 中期费用提供方法
	 * create by aaron.lu
	 * create at 2013-3-11 下午9:00:04
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getInterimFee(Page<InterimFeeVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		
		Integer trainer_car_id_ = (Integer)params.get("trainer_car_id_");
		
		if (null != trainer_car_id_) {
			
			List<InterimFeeVo> list = trainerCarService.queryInterimFee(params, limmit, offset);
			int total = trainerCarService.countInterimFee(params);
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("无教练车ID");
			
			page.setEntities(new ArrayList<InterimFeeVo>());
			page.setEntityCount(0);
		}
	}
	
	/**
	 * 维修费用提供方法
	 * create by aaron.lu
	 * create at 2013-3-11 下午9:01:26
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getRepairFee(Page<RepairFeeVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		
		Integer trainer_car_id_ = (Integer)params.get("trainer_car_id_");
		
		if (null != trainer_car_id_) {
			
			List<RepairFeeVo> list = trainerCarService.queryRepairFee(params, limmit, offset);
			int total = trainerCarService.countRepairFee(params);
			page.setEntities(list);
			page.setEntityCount(total);
		}else{
			logger_.error("无教练车ID");
			
			page.setEntities(new ArrayList<RepairFeeVo>());
			page.setEntityCount(0);
		}
	}
	
	/**
	 * 油款操作方法
	 * create by aaron.lu
	 * create at 2013-3-11 下午9:03:58
	 * <mailto: atyuan.lu@gmail.com>
	 * @param fuelBillsVos
	 *
	 */
	@DataResolver
	public void FuelBillsHandler(Collection<FuelBillsVo> fuelBillsVos){
		for (FuelBillsVo fuelBillsVo : fuelBillsVos) {
			EntityState entityState = EntityUtils.getState(fuelBillsVo);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				trainerCarService.insertFuelBills(fuelBillsVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				//DO NOTHING
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				trainerCarService.deleteFuelBills(fuelBillsVo.getFuelBillsId());
			}
		}
	}
	
	/**
	 * 中期费用操作方法
	 * create by aaron.lu
	 * create at 2013-3-11 下午9:06:10
	 * <mailto: atyuan.lu@gmail.com>
	 * @param interimFeeVos
	 *
	 */
	@DataResolver
	public void RepairFeeHandler(Collection<RepairFeeVo> repairFeeVos){
		for (RepairFeeVo repairFeeVo : repairFeeVos) {
			EntityState entityState = EntityUtils.getState(repairFeeVo);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				trainerCarService.insertRepairFee(repairFeeVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				//DO NOTHING
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				trainerCarService.deleteRepairFee(repairFeeVo.getRepairFeeId());
			}
		}
	}
	
	@DataResolver
	public void InterimFeeHandler(Collection<InterimFeeVo> interimFeeVos){
		for (InterimFeeVo interimFeeVo : interimFeeVos) {
			EntityState entityState = EntityUtils.getState(interimFeeVo);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				trainerCarService.insertInterimFee(interimFeeVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				//DO NOTHING
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				trainerCarService.deleteInterimFee(interimFeeVo.getInterimFeeId());
			}
		}
	}
	
	
	
	
	/**
	 * 保存教练车数据
	 * create by aaron.lu
	 * create at 2012-3-21 下午10:26:26
	 * <mailto: atyuan.lu@gmail.com>
	 * @param trainerCarVo
	 *
	 */
	private void insertTrainerCar(TrainerCarVo trainerCarVo){
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			trainerCarVo.setTrainerCarStatus(Constant.INT_TRUE_VALUE.toString());
			trainerCarVo.setCreateUser(accountVo.getAccountLoginName());
			trainerCarVo.setCreateTime(new Date());
			trainerCarVo.setLastModifyTime(new Date());
			trainerCarVo.setLastModifyUser(accountVo.getAccountLoginName());
			
			//设置所属的公司数据
			trainerCarVo.setCompanyId(accountVo.getCompanyId());
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}
		
		trainerCarService.insertTrainerCar(trainerCarVo);
	}
	
	/**
	 * 更新教练车
	 * create by aaron.lu
	 * create at 2012-3-21 下午10:27:31
	 * <mailto: atyuan.lu@gmail.com>
	 * @param trainerCarVo
	 *
	 */
	private void updateTrainerCar(TrainerCarVo trainerCarVo){
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			trainerCarVo.setLastModifyTime(new Date());
			trainerCarVo.setLastModifyUser(accountVo.getAccountLoginName());
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}
		
		trainerCarService.updateTrainerCar(trainerCarVo);
	}
	
	/**
	 * 禁用教练车
	 * create by aaron.lu
	 * create at 2012-3-22 下午2:28:41
	 * <mailto: atyuan.lu@gmail.com>
	 * @param trainerCarVo
	 *
	 */
	private void disableTrainerCar(TrainerCarVo trainerCarVo){
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			trainerCarVo.setTrainerCarStatus(Constant.INT_FALSE_VALUE.toString());//设置禁用状态为禁用
			trainerCarVo.setLastModifyTime(new Date());
			trainerCarVo.setLastModifyUser(accountVo.getAccountLoginName());
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}
		
		trainerCarService.updateTrainerCar(trainerCarVo);
	}
	
	
	
}
